<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <style>
        * {
            box-sizing: border-box;
        }
        body {
            font-family: "Helvetica Neue", Arial, sans-serif;
            background: linear-gradient(135deg, #e0f7fa 0%, #b2ebf2 100%);
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            padding: 0 20px;
        }
        .login-container {
            background: #ffffff;
            border-radius: 12px;
            padding: 40px;
            box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
            text-align: center;
            animation: fadeIn 0.5s ease-in-out;
        }
        @keyframes fadeIn {
            from {
                opacity: 0;
            }
            to {
                opacity: 1;
            }
        }
        h2 {
            margin-bottom: 30px;
            color: #007aff;
            font-size: 28px;
        }
        input {
            width: 100%;
            padding: 15px;
            margin-bottom: 15px;
            border: 1px solid #007aff;
            border-radius: 5px;
            font-size: 16px;
            transition: border 0.3s ease;
        }
        input:focus {
            border-color: #0051a8;
            outline: none;
        }
        button {
            width: 100%;
            padding: 15px;
            background-color: #007aff;
            color: #ffffff;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        button:hover {
            background-color: #0051a8;
        }
        .message {
            margin-top: 15px;
            color: #d9534f; /* 红色错误信息 */
            display: none; /* 默认隐藏 */
        }
        footer {
            margin-top: 20px;
            font-size: 14px;
            color: #666;
        }
        @media (max-width: 480px) {
            .login-container {
                padding: 30px;
            }
            h2 {
                font-size: 24px;
            }
            input, button {
                font-size: 14px;
            }
        }
    </style>
</head>
<body>

    <div class="login-container">
        <h2>用户登录</h2>
        <form id="loginForm" onsubmit="return login(event)">
            <input type="text" id="username" placeholder="用户名" required>
            <input type="password" id="password" placeholder="密码" required>
            <button type="submit">登录</button>
        </form>
        <div class="message" id="message"></div>
        <footer>
            <p>© JiangChu. All Rights Reserved</p>
        </footer>
    </div>

    <script>
        async function login(event) {
            event.preventDefault();
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const messageDiv = document.getElementById('message');

            const response = await fetch('/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ username, password }),
            });

            if (response.ok) {
                window.location.href = '/admin'; // 成功登录后重定向到后台管理
            } else {
                messageDiv.innerHTML = '用户名或密码错误！';
                messageDiv.style.display = 'block'; // 显示错误信息
            }
        }
    </script>
</body>
</html>
